<!--
 * @Description:
 * @Author: ZachGmy
 * @Date: 2020-09-03 09:57:40
 * @LastEditors: ZachGmy
 * @LastEditTime: 2022-06-15 09:49:13
-->
<template>
  <div>
    <!-- 顶部新增按钮 -->
    <div class="addBtn">
      <el-button type="primary" @click="toAddNews">新增</el-button>
    </div>
    <!-- 表格内容展示区 -->
    <div class="content">
      <el-table :data="newsData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column prop="no" label="序号" width="120" align="center">
        </el-table-column>
        <!-- 分类名称 -->
        <el-table-column
          prop="name"
          label="分类名称"
          width="120"
          align="center"
        >
        </el-table-column>
        <!-- 分类简介 -->
        <el-table-column prop="description" label="分类简介" align="center">
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button @click="toEditNews(scope.row)" type="text" size="small"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog
      v-if="dialogVisible"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="80%"
    >
      <!-- 模态框内容 -->
      <el-form
        :model="newsForm"
        :rules="newsRules"
        ref="newsForm"
        label-width="100px"
      >
        <!-- 名称 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="newsForm.name"></el-input>
        </el-form-item>
        <!-- 介绍 -->
        <el-form-item label="介绍" prop="description">
          <el-input type="textarea" v-model="newsForm.description"></el-input>
        </el-form-item>
        <!-- 序号 -->
        <el-form-item label="序号" prop="no">
          <el-input v-model="newsForm.no"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmitNews">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      // 资讯表单对象
      newsForm: {},
      // 资讯表单的验证规则
      newsRules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        no: [{ required: true, message: "请输入序号", trigger: "blur" }],
      },
      // 资讯数据数组
      newsData: [],
      // 控制模态框的显示与隐藏
      dialogVisible: false,
      // 设置模态框的标题
      dialogTitle: "",
    };
  },
  created() {
    // 自动获取资讯数据
    this.getNewsData();
  },
  methods: {
    // 提交或修改资讯数据
    toSubmitNews() {
      this.$refs["newsForm"].validate(async (valid) => {
        if (valid) {
          let res = await post("/category/saveOrUpdate", this.newsForm);
          if (res.status == 200) {
            this.$notify({
              title: "成功",
              message: res.me,
              type: "success",
            });
            this.getNewsData()
            this.dialogVisible = false
          }else{
            this.$notify({
              title: "失败",
              message: res.message,
              type: "error",
            });
          }
        } else {
          return false;
        }
      });
    },
    // 新增资讯
    toAddNews() {
      // 设置模态框的标题
      this.dialogTitle = "录入分类信息";
      // 清空表单对象中的数据
      this.newsForm = {};
      // 弹出模态框
      this.dialogVisible = true;
    },
    // 编辑资讯
    toEditNews(row) {
      // 设置模态框的标题
      this.dialogTitle = "修改分类信息";
      // 将当前点击的这一行的数据赋值到模态框里
      this.newsForm = { ...row };
      // 弹出模态框
      this.dialogVisible = true;
    },
    // 删除资讯
    toDelete(id) {
      // 消息提示的确认
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确定按钮执行的方法
        .then(async () => {
          // 发送请求
          let res = await get("/category/deleteById", { id });
          // console.log(res)
          if (res.status == 200) {
            // 请求成功给用户一个提示
            this.$message({
              type: "success",
              message: res.message,
            });
            // 刷新页面数据
            this.getNewsData();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!",
            });
          }
        })
        // 点击取消按钮执行的方法
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取所有资讯数据
    async getNewsData() {
      let res = await get("/category/findAll");
      // console.log(res.data)
      this.newsData = res.data;
    },
  },
};
</script>

<style scoped>
</style>
